using System.Web.Security;
using Castle.MonoRail.Framework;
using WineCellar.Common;

namespace WineCellar.Common.Controllers
{
    [Layout("Admin")]
    [ControllerDetails("Role")]
    public class RoleMaintainController:SmartDispatcherController
    {
        private class PropertyAccessor : PropertyAccessorBase
        {
            public class Fields
            {
			    public const string Roles= "roles";
			    public const string Role= "role";
            }
            public PropertyAccessor(Controller controller)
            {
                this.Initialize(controller);
            }
            public string[] Roles
            {
                get { return (string[])this.Flash[Fields.Roles]; }
                set { this.Flash[Fields.Roles]=value; }
            }
            public string Role
            {
                get { return (string)this.Flash[Fields.Role]; }
                set { this.Flash[Fields.Role] = value; }
            }
        }
        private PropertyAccessor _propertyAccessor;
        protected override void Initialize()
        {
            base.Initialize();
            this._propertyAccessor = new PropertyAccessor(this);

        }
        public void List()
        {
            this._propertyAccessor.Roles= Roles.GetAllRoles();
            

        }
        public void ShowAdd()
        {

        }
        public void ShowDelete(string roleName)
        {
            this._propertyAccessor.Role = roleName;
        }
        public void Delete(string role)
        {
            Roles.DeleteRole(role);
            this._propertyAccessor.AddMessage(new Message(string.Format("Role {0} deleted.", role), Message.Enums.MessageClass.Information));
            RedirectToAction("List");
        }
        public void Add(string roleName)
        {
            if (roleName.Trim() == string.Empty)
            {
                this._propertyAccessor.AddMessage(new Message("Role name cannot be empty.", Message.Enums.MessageClass.Warning));
                RedirectToAction("ShowAdd");
            }
            else
            {
                if (!Roles.RoleExists(roleName))
                    Roles.CreateRole(roleName);
                else
                    this._propertyAccessor.AddMessage(new Message(string.Format("Role {0} already exist.", roleName), Message.Enums.MessageClass.Warning));
                RedirectToAction("List");
            }
        }
        
    }
}
